package com.mini.config;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * <p>webmvc配置</p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author zl
 * @version 1.0
 * @package: com.sunlands.zlcx.mpmsgservice.config
 * @date Created in 2017/11/30 15:41
 * @copyright: Copyright (c) founders
 */
@Configuration
public class WebAppConfigurer extends WebMvcConfigurerAdapter {

//    @Autowired
//    private SignInterceptor signInterceptor;

    @Autowired
    private CheckloginInterceptor checkloginInterceptor;

    @Value("${mobileEncryptKey}")
    private String mobileEncryptKey;

    @Value("${sunlands.bf.report.bfEncryptKey}")
    private String bfEncryptKey;

    @Bean
    ObjectMapper objectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        return objectMapper;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
//        registry.addInterceptor(signInterceptor)
//                .addPathPatterns("/v1/**");
        registry.addInterceptor(checkloginInterceptor)
                .addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    /**
     * @Autowired
     * private AES aes;
     * 加密 aes.encryptHex(phone);
     * 解密 aes.decryptStr(encryptStr)
     */
    @Bean
    AES aes(){
        return SecureUtil.aes(mobileEncryptKey.getBytes());
    }


    /**
     * 上报集团BF使用的密钥
     * @return
     */
    @Bean
    AES bfAes(){
        return SecureUtil.aes(bfEncryptKey.getBytes());
    }
}
